/*
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.core.data.entity;

import java.util.ArrayList;
import java.util.List;
import traderlib.core.data.types.KeyPointer;
import traderlib.core.data.types.Value;

/**
 * A field list packs a list of field, its field map, the primary key pointers and the order key pointers if applicable.
 *
 * @author Miquel Sas
 */
public class FieldList {

	/**
	 * The field map to access fields by alias.
	 */
	private transient FieldMap fieldMap = null;
	/**
	 * The list of fields.
	 */
	private final List<Field> fields = new ArrayList<>();
	/**
	 * The list of persistent fields.
	 */
	private List<Field> persistentFields;
	/**
	 * The list of primary key pointers.
	 */
	private List<KeyPointer> primaryKeyPointers = null;
	/**
	 * The list of persistent key pointers.
	 */
	private List<KeyPointer> persistentKeyPointers = null;

	/**
	 * Default constructor.
	 */
	public FieldList() {
		super();
	}

	/**
	 * Clear this field list.
	 */
	public void clear() {
		fields.clear();
		clearFieldMap();
	}

	/**
	 * Returns this field listt size.
	 *
	 * @return The number of fields.
	 */
	public int size() {
		return fields.size();
	}

	/**
	 * Clear the field map, necessary when changing fields.
	 */
	public void clearFieldMap() {
		FieldMap.removeMap(fieldMap);
		fieldMap = null;
		primaryKeyPointers.clear();
		primaryKeyPointers = null;
		persistentFields.clear();
		persistentFields = null;
		persistentKeyPointers.clear();
		persistentKeyPointers = null;
	}

	/**
	 * Returns this field map.
	 *
	 * @return The field map.
	 */
	public FieldMap getFieldMap() {
		if (fieldMap == null) {
			fieldMap = new FieldMap(fields);
		}
		return fieldMap;
	}

	/**
	 * Returns the list of default values.
	 *
	 * @return The list of values.
	 */
	public List<Value> getDefaultValues() {
		List<Value> values = new ArrayList<>();
		for (Field field : fields) {
			values.add(field.getDefaultValue());
		}
		return values;
	}

	/**
	 * Add a field to the field list.
	 *
	 * @param field The field to add.
	 */
	public void addField(Field field) {
		fields.add(field);
		clearFieldMap();
	}

	/**
	 * Returns the number of fields in this table.
	 *
	 * @return The number of fields.
	 */
	public int getFieldCount() {
		return fields.size();
	}

	/**
	 * Returns the field in the given index.
	 *
	 * @param index The index of the field.
	 * @return The field
	 */
	public Field getField(int index) {
		return fields.get(index);
	}

	/**
	 * Get a field by alias.
	 *
	 * @return The field or null if not found.
	 * @param alias The field alias.
	 */
	public Field getField(String alias) {
		int index = getFieldIndex(alias);
		return (index == -1 ? null : FieldList.this.getField(index));
	}

	/**
	 * Get a field index by alias.
	 *
	 * @return The field index or -1 if not found.
	 * @param alias The field alias.
	 */
	public int getFieldIndex(String alias) {
		return getFieldMap().getFieldIndex(alias);
	}

	/**
	 * Get a field index.
	 *
	 * @return The field index or -1 if not found.
	 * @param field The field.
	 */
	public int getFieldIndex(Field field) {
		return getFieldIndex(field.getAlias());
	}

	/**
	 * Returns the internal list of fields.
	 *
	 * @return
	 */
	public List<Field> getFields() {
		return fields;
	}

	/**
	 * Returns the list of persistent fields.
	 *
	 * @return The list of persistent fields.
	 */
	public List<Field> getPersistentFields() {
		if (persistentFields == null) {
			persistentFields = new ArrayList<>();
			for (Field field : fields) {
				if (field.isPersistent()) {
					persistentFields.add(field);
				}
			}
		}
		return persistentFields;
	}

	/**
	 * Returns the list of persistent key pointers.
	 *
	 * @return The list of persistent key pointers.
	 */
	public List<KeyPointer> getPersistentKeyPointers() {
		if (persistentKeyPointers == null) {
			persistentKeyPointers = new ArrayList<>();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (field.isPersistent()) {
					persistentKeyPointers.add(new KeyPointer(i));
				}
			}
		}
		return persistentKeyPointers;
	}

	/**
	 * Removes all the fields in this table.
	 *
	 * @return The removed fields
	 */
	public List<Field> removeAllFields() {
		clearFieldMap();
		List<Field> removedFields = new ArrayList<>();
		removedFields.addAll(fields);
		fields.clear();
		return removedFields;
	}

	/**
	 * Remove the field at the given index.
	 *
	 * @param index The index of the field
	 * @return The removed field or null.
	 */
	public Field removeField(int index) {
		clearFieldMap();
		return fields.remove(index);
	}

	/**
	 * Remove the field with the given alias.
	 *
	 * @param alias The alias of the field
	 * @return The removed field or null.
	 */
	public Field removeField(String alias) {
		int index = getFieldIndex(alias);
		if (index < 0) {
			return null;
		}
		return FieldList.this.removeField(index);
	}

	/**
	 * Returns the list of primary key fields.
	 *
	 * @return The list of primary key fields.
	 */
	public List<Field> getPrimaryKeyFields() {
		List<Field> primaryKeyFields = new ArrayList<>();
		for (Field field : fields) {
			if (field.isPrimaryKey()) {
				primaryKeyFields.add(field);
			}
		}
		return primaryKeyFields;
	}

	/**
	 * Returns the list of primary key pointers.
	 *
	 * @return The list of primary key pointers.
	 */
	public List<KeyPointer> getPrimaryKeyPointers() {
		if (primaryKeyPointers == null) {
			primaryKeyPointers = new ArrayList<>();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (field.isPrimaryKey()) {
					primaryKeyPointers.add(new KeyPointer(i, true));
				}
			}
			if (primaryKeyPointers.isEmpty()) {
				for (int i = 0; i < fields.size(); i++) {
					primaryKeyPointers.add(new KeyPointer(i, true));
				}
			}
		}
		return primaryKeyPointers;
	}

}
